﻿

using System;
using AutoMapper;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

using Oliving.Edge.AuthHelper;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IServices;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.IServices.MQXT;
using System.Linq.Expressions;
using Oliving.Edge.Model.Models.MQXT;
using Oliving.Edge.Model.Dto.MQXT;
using Oliving.Edge.Common.Extensions;

namespace Oliving.Edge.Controllers
{
    [ApiController]
    [Route("api/MQXT_CaiJiQiModel")]
    [Authorize(Permissions.Name)]
    public class MQXT_CaiJiQiModelController : OlivingControll
    {
        IMapper _mapper;
        private readonly IMQXT_CaiJiQiModelServices _MQXT_CaiJiQiModelServices;
        private readonly ILogger<MQXT_CaiJiQiModelController> _logger;


        public MQXT_CaiJiQiModelController(
                IHttpContextAccessor accessor, PermissionRequirement requirement,
                IMapper mapper, ILogger<MQXT_CaiJiQiModelController> logger,
                IMQXT_CaiJiQiModelServices MQXT_CaiJiQiModelServices
            ) : base(accessor, requirement)
        {
            _logger = logger;
            _mapper = mapper;
            _MQXT_CaiJiQiModelServices = MQXT_CaiJiQiModelServices;
        }



        [HttpGet]
        [AllowAnonymous]
        [Route("/api/[controller]/Get")]
        public async Task<MessageModel<MQXT_CaiJiQiModelDto>> Get(string id)
        {
            var data = new MessageModel<MQXT_CaiJiQiModelDto>();
            MQXT_CaiJiQiModel model = await _MQXT_CaiJiQiModelServices.QueryById(id);
            if (model == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = _mapper.Map<MQXT_CaiJiQiModelDto>(model);
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }
            return data;
        }




        [HttpGet]
        [Route("/api/[controller]/GetPageList")]
        public async Task<MessageModel<PageModel<MQXT_CaiJiQiModelPageDto>>> GetPageList(int page = 1, int PageSize = 20, string sidx = "", string order = ""
            , string key = "", string yb = "")
        {
            Expression<Func<MQXT_CaiJiQiModel, bool>> whereExpression = _MQXT_CaiJiQiModel => true;
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            else
            {
                whereExpression = whereExpression.And(d => d.CJQ_Addr.Contains(key) || d.CJQ_SIM.Contains(key) || d.CJQ_Yb.Contains(key));
            }

            if (string.IsNullOrEmpty(yb) || string.IsNullOrWhiteSpace(yb))
            {

            }
            else
            {
                whereExpression = whereExpression.And(d => d.CJQ_Yb.Contains(yb));
            }


            var data = await _MQXT_CaiJiQiModelServices.QueryPageList(whereExpression, page, PageSize, sidx, order);

            return new MessageModel<PageModel<MQXT_CaiJiQiModelPageDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }



        [HttpPut]
        [Route("/api/[controller]/Update")]
        public async Task<MessageModel<string>> Update([FromBody] MQXT_CaiJiQiModelDto dto)
        {
            var data = new MessageModel<string>();

            if (dto != null && dto.CJQ_Id > 0)
            {
                MQXT_CaiJiQiModel model = await _MQXT_CaiJiQiModelServices.QueryById(dto.CJQ_Id);
                if (model != null)
                {
                    model.CJQ_Addr = dto.CJQ_Addr;
                    model.CJQ_SIM = dto.CJQ_SIM;
                    model.CJQ_Yb = dto.CJQ_Yb;
                    model.CJQ_GPS = dto.CJQ_GPS;
                    model.CJQ_Ver = dto.CJQ_Ver;
                    model.CJQ_Count = dto.CJQ_Count;
                    model.CJQ_JCQ1 = dto.CJQ_JCQ1;
                    model.CJQ_JCQ2 = dto.CJQ_JCQ2;
                    model.CJQ_JCQ3 = dto.CJQ_JCQ3;
                    model.CJQ_JCQ4 = dto.CJQ_JCQ4;
                    model.CJQ_JCQ5 = dto.CJQ_JCQ5;
                    model.CJQ_JCQ6 = dto.CJQ_JCQ6;
                    model.CJQ_JCQ7 = dto.CJQ_JCQ7;
                    model.CJQ_JCQ8 = dto.CJQ_JCQ8;
                    model.CJQ_JCQ9 = dto.CJQ_JCQ9;
                    model.CJQ_JCQ10 = dto.CJQ_JCQ10;
                    model.UpdateTIme = dto.UpdateTIme;


                    data.success = await _MQXT_CaiJiQiModelServices.Update(model);

                    if (data.success)
                    {
                        data.msg = "更新成功";
                        data.response = dto.CJQ_Id.ObjToString();
                    }
                }
                else
                {
                    data.success = false;
                    data.msg = "参数异常，您要更新的数据不存在";
                }
            }
            else
            {
                data.success = false;
                data.msg = "参数异常";
            }
            return data;
        }





        [HttpPost]
        [Route("/api/[controller]/Add")]
        public async Task<MessageModel<string>> Add(MQXT_CaiJiQiModel dto)
        {
            var data = new MessageModel<string>();
            int id = await _MQXT_CaiJiQiModelServices.AddGuid(dto);

            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
            return data;
        }




        [HttpDelete]
        [Route("/api/[controller]/Delete")]
        public async Task<MessageModel<string>> Delete(string id)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(id))
            {
                data.success = await _MQXT_CaiJiQiModelServices.DeleteById(id);

                if (data.success)
                {
                    data.msg = "删除成功";
                    data.response = data.success.ObjToString(); 
                }
            }
            return data;
        }





        [HttpDelete]
        [Route("/api/[controller]/BatchDelete")]
        public async Task<MessageModel<string>> BatchDelete(string ids)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(ids))
            {
                if (false)
                {
                    #region 软删除

                    int[] list = Array.ConvertAll<string, int>(ids.Split(','), s => int.Parse(s));

                    MQXT_CaiJiQiModel model = new MQXT_CaiJiQiModel();
                    //  model.C_State = -1;          ???????????????????
                    string whereStr = " CJQ_Id in (" + ids + ")";
                    List<string> colList = new List<string>();
                    //  colList.Add("C_State");    ???????????????????
                    List<string> colHL = new List<string>();

                    bool isOK = await _MQXT_CaiJiQiModelServices.Update(model, colList, colHL, whereStr);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }

                    #endregion
                }
                {
                    #region 直接删除 

                    object[] list = Array.ConvertAll<string, object>(ids.Split(','), s => int.Parse(s));

                    bool isOK = await _MQXT_CaiJiQiModelServices.DeleteByIds(list);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }
                    #endregion
                }
            }
            return data;
        }




    }
}

